import config from '../../etc/config.js'
const app = getApp()
const {
  $Toast
} = require('../../components/dist/base/index');

Page({
  data: {
    balance: 0,
    freeze: 0,
    score: 0,
    score_sign_continuous: 0,
    userInfo: {},
    showModal: true
  },
  onLoad(){
    wx.hideShareMenu()
  },
  
  onShow() {
    // let token = wx.getStorageSync('token');
    let loginTime = wx.getStorageSync('loginTime')
    let time = wx.getStorageSync('times')
    // 检查token和过期时间是否有效
    // if (time > Date.parse(new Date()) / 1000) {
    //   console.log()
    //   // this.login()
    //   this.setData({
    //     show:true
    //   })
    // } else {
    //   this.registerUser()
    // }
    // if (time > loginTime) {
      // console.log('qawcf', time)
      // this.login()
      this.getUserInfo()
    // } else {
      // this.registerUser()
    // }
  },
  cancelCs(e) {
    let webview = e.target.dataset.webview
    wx.navigateTo({
      url: '../kkk/index?webview=' + encodeURIComponent(webview),
      success: function (res) { },
      fail: function (res) { },
      complete: function (res) { },
    })
    // window.location.href = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx64765deefa1a2ab5&redirect_uri=https%3a%2f%2fhqadmin.yuzhekeji.top%2fwechatApi%2fcode%2fgetOpenId&response_type=code&userId='+userId + '&scope=snsapi_base&state=STATE#wechat_redirect'
    // wx.navigateTo({
    //   url: 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx64765deefa1a2ab5&redirect_uri=https%3a%2f%2fhqadmin.yuzhekeji.top%2fwechatApi%2fcode%2fgetOpenId&response_type=code&userId=' + userId + '&scope=snsapi_base&state=STATE#wechat_redirect',
    // })
  },
  getUserInfo() {
    let params = {
      userId: wx.getStorageSync('userId')
    }
    let token = wx.getStorageSync('token')
    config._post('/user/getUserInfo', token, params).then(res => {
      console.log(res)
      if (res.data) {
        // 用户基本信息展示
        this.setData({
          userInfo: res.data,
          // version: app.globalData.version
        })
      }
    })
  },
  goDelivery(e) {
    let userInfo = this.data.userInfo
    if (userInfo.userIsDeliveryman === 1) {
      wx.navigateTo({
        url: '../delivery-center/index?deId=' + userInfo.deId,
        success: function(res) {},
        fail: function(res) {},
        complete: function(res) {},
      })
    } else {
      if (userInfo.checkStatus === 0) {
        $Toast({
          content: '申请已提交，请等待审核'
        })
        return false
      } else if (userInfo.checkStatus === 2) {
        let params = {
          userId: wx.getStorageSync('userId')
        }
        let token = wx.getStorageSync('token')
        config._post('/user/getUserInfoDe', token, params).then(res => {
          this.showDialogBtn()
          this.setData({
            reason: res.data.deliveryman.reason,
            value: res.data.deliveryman
          })
        })
      } else {
        wx.navigateTo({
          url: '../register/index',
        })
      }
    }
  },
  /**
   * 弹窗
   */
  showDialogBtn: function() {
    this.setData({
      showModal: false
    })
  },
  /**
   * 弹出框蒙层截断touchmove事件
   */
  preventTouchMove: function() {},
  /**
   * 隐藏模态对话框
   */
  hideModal: function() {
    this.setData({
      showModal: true
    });
  },
  /**
   * 对话框取消按钮点击事件
   */
  onCancel: function() {
    this.hideModal();
  },
  /**
   * 对话框确认按钮点击事件
   */
  onConfirm: function() {
    this.hideModal()
    wx.navigateTo({
      url: '../register/index?value=' + JSON.stringify(this.data.value),
    })
  }
})